
import java.util.TreeSet;
import java.util.Stack;


public class PointSET {

	private TreeSet<Point2D> points;
	
	public PointSET() {                              // construct an empty set of points
		points = new TreeSet<Point2D>();
	}
	
	public boolean isEmpty() {                        // is the set empty?
		return points.isEmpty();
	}
	
	public int size() {                              // number of points in the set
		return points.size();
	}
	
	public void insert(Point2D p) {                  // add the point p to the set (if it is not already in the set)
		if (!points.contains(p))
			points.add(p);
	}
	
	public boolean contains(Point2D p) {             // does the set contain the point p?
		return points.contains(p);
	}
	
	public void draw() {                             // draw all of the points to standard draw
		for (Point2D p: points)
			p.draw();
	}
	
	public Iterable<Point2D> range(RectHV rect) {    // all points in the set that are inside the rectangle
		Stack<Point2D> stack = new Stack<Point2D>();
		
		for (Point2D p: points) {
			if (rect.contains(p))
				stack.push(p);
		}
		
		return stack;
	}
	
	public Point2D nearest(Point2D p) {               // a nearest neighbor in the set to p; null if set is empty
		double nearest = 2.0;
		Point2D np = null;
		
		for (Point2D p2: points) {
			if (p2.distanceTo(p) < nearest) {
				nearest = p2.distanceTo(p);
				np = p2;
			}
				
		}
		
		return np;
	}
   
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
